var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tm:[],
    loading: false,
    plain: false,
    disabled: false,//按钮一旦点击提交 则不能操作
    activeindex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }
    this.setData({
      pid: options.pid,
      usid: usid
    })
    this.getScdhTmByPid();
  },

  getScdhTmByPid(){
    var that = this;
    var pid = that.data.pid;
    app.wxget({
      url: 'wscdh/getScdhTmByPid.html',
      data: {
        pid: pid,
      },
      success: function (res) {
        var datas = res.data;
        var tmarr=[];
        for (var i = 0; i < datas.length;i++){
          var ts={
            QID: datas[i].QID,
            XH: datas[i].XH,
            TMNR: datas[i].TMNR,
            TX: datas[i].TX,
            TXT: datas[i].TXT,
            ANSWER: datas[i].ANSWER,
            JF: datas[i].JF,
            AD: datas[i].AD,
            BD: datas[i].BD,
            CD: datas[i].CD,
            DD: datas[i].DD,
            ED: datas[i].ED,
            FD: datas[i].FD,
            PXH: datas[i].PXH,
            MYDA: ''
          }
          tmarr.push(ts);
        }
        that.setData({
          tm: tmarr
        }) 
        
      }
    })
  },

  //下一题
  nexttm(){
    var activeindex=this.data.activeindex;
    var i = parseInt(activeindex)+1;
    this.setData({
      activeindex: i
    }) 
  },
  //上一题
  pretm() {
    var activeindex = this.data.activeindex;
    var i = parseInt(activeindex)-1;
    this.setData({
      activeindex: i
    })
  },

  //选择答案
  selectanswer(e) {
    var index = e.currentTarget.dataset.index;
    var da = e.currentTarget.dataset.da;
    var tm=this.data.tm;
    tm[index].MYDA = da;
    this.setData({
      tm: tm
    })
  }, 


  //提交答案
  saveAnswer() {
    var tm = this.data.tm;
    var daarr=[];
    for(var i=0;i<tm.length;i++){
      if (tm[i].MYDA){
        var ds = {
          qid: tm[i].QID,
          bh: tm[i].MYDA,
          answer: tm[i].MYDA
        }
        daarr.push(ds);
      } 
    }
    if (daarr.length<=0){
      wx.showModal({
        title: '错误',
        content: '请选择答案！',
        showCancel: !1,
      });
      return;
    }
    var usid = wx.getStorageSync('usid');
    var username = wx.getStorageSync('name');
    var da = JSON.stringify(daarr);
    wx.showLoading({
      title: '保存中',
    })
    var that=this;
    app.wxpost({
      url: 'wscdh/insertBookanswer.html',
      data: {
        da: da,
        usid: usid,
        username:username
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.result) {
          wx.showModal({
            title: '成功',
            content: res.data.msg,
            showCancel: !1,
          });
          //跳转到成功页面
          wx.redirectTo({
            url: '../../pages/scdhcg/scdhcg?bid=' + that.data.pid
          })
        } else {
          wx.showModal({
            title: '失败',
            content: res.data.msg,
            showCancel: !1,
          });
        }
      }
    })

  }, 

})